# pagination.py
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class StandardResultsSetPagination(PageNumberPagination):
    # 默认每页数量
    page_size = 10
    # URL 上允许指定每页数量 ?page_size=20
    page_size_query_param = "page_size"
    # 限制最大每页数量
    max_page_size = 100

    def get_paginated_response(self, data):
        """
        自定义返回格式
        """
        return Response(
            {
                "pagination": {
                    "total": self.page.paginator.count,  # 总数
                    "page": self.page.number,  # 当前页码
                    "page_size": self.get_page_size(self.request),  # 每页数量
                    "total_pages": self.page.paginator.num_pages,  # 总页数
                },
                "list": data,
            }
        )
